package com.example.showImage;

import com.example.photoview.ControllerUtil;
import com.example.photoview.ImageModel;
import com.example.showImage.ImageController;

import java.io.IOException;
import java.util.ArrayList;
/**
 *
 * 切换上下张图片的类
 * lastOne（上一张）、nextOne（下一张）
 * */
public class SwitchPictures {
    //实现上下张图片的切换
    private ArrayList<ImageModel> imgList;
    public ImageController dw;
    private double spinTime=0;
    public SwitchPictures() {
        dw = (ImageController) ControllerUtil.controllers.get(ImageController.class.getSimpleName());//获取已经存储的控制类
        //snackbar = new JFXSnackbar(dw.getRootPane());
    }
    //有参构造方法
    public SwitchPictures(ArrayList<ImageModel> imageModelArrayList) {
        this();
        this.imgList = imageModelArrayList;
    }


    /**返回下一张照片*/
    public ImageModel nextOne(ImageModel im) throws IOException {
        int i = 0;
        //System.out.println("nextOne function of imgList:"+imgList);
        for (i = 0; i < imgList.size(); i++) {
            if (imgList.get(i).getImageName().equals(im.getImageName())) {
                if (i == imgList.size() - 1) {
                    System.out.println("已到达最后一张照片！");
                    if (AlertModel.returnAlertQuestion_ToFirst("已到达最后一张!是否观看第一张照片？")==1) {
                        i=-1;//-1+1=0
                    }else{
                        //就算不是也会超频率
                        i-=1;//i-1+1=i
                    }
                }
                break;
            }
        }//for
        return imgList.get((i+1)% (imgList.size())) ;//Index -1 out of bounds for length 32
    }


    /**返回下一张照片，但是幻灯片放映模式专用*/
    public ImageModel nextOne_NoAlert(ImageModel im) throws IOException {
        int i = 0;
        //System.out.println("nextOne function of imgList:"+imgList);
        for (i = 0; i < imgList.size(); i++) {
            if (imgList.get(i).getImageName().equals(im.getImageName())) {
                if (i == imgList.size() - 1) {
                    System.out.println("已到达最后一张照片！");
                    i=-1;//-1+1=0
                }
                break;
            }
        }//for
        return imgList.get((i+1)% (imgList.size())) ;//Index -1 out of bounds for length 32
    }

    /**
     * 返回上一张照片
     * */
    public ImageModel lastOne(ImageModel im) throws IOException {
        int i=0;
        //System.out.println("lastOne function of imgList:"+imgList);
        for (i=0;i<imgList.size();i++) {
            if (imgList.get(i).getImageName().equals(im.getImageName())) {
                if (i == 0) {
                    System.out.println("已到达第一张照片!");
                    if(AlertModel.returnAlertQuestion_ToLast("已到达第一张照片!是否观看最后一张照片？")==-1){
                        i=imgList.size();//max-1
                        //是否到最后一张 yes==-1 no==0
                    }else{
                        //避免到-1去
                        i+=1;//0+1-1=0
                    }
                }
                break;
            }
        }
        return imgList.get((i-1)% (imgList.size()));//Index -1 out of bounds for length 32
    }

    /**
     * 左转
     * @return
     */
    public double leftSpin() {
        spinTime=(spinTime-1)%4;
        System.out.println(spinTime);
        return spinTime*90;
    }

    /**
     * 右转
     * @return
     */
    public double rightSpin() {
        spinTime=(spinTime+1)%4;
        System.out.println(spinTime);
        return spinTime*90;
    }
}
